package com.keyancompany.zhongbaopingtai.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.CircleModel;
import com.keyancompany.zhongbaopingtai.model.TeamUser;
import com.keyancompany.zhongbaopingtai.weight.CircleOptionsPopup;
import com.keyancompany.zhongbaopingtai.weight.TeamOptionsPopup;

/**
 * 我的圈子适配器 or  我参与的圈子
 * <p>
 * Created by firefox on 2016/6/18.
 */
public class MyCircleAdapter extends ListAdapter<CircleModel> {

    CircleOptionsPopup circleOptionsPopup;
    private boolean isSearch = false;
    private View.OnClickListener joinListener;

    public void setSearch(boolean search, View.OnClickListener listener) {
        isSearch = search;
        joinListener = listener;
    }

    public MyCircleAdapter(Context ctx) {
        super(ctx);
    }

    @Override
    public int getContentViewId() {
        return R.layout.item_team_circle;
    }

    @Override
    public HolderView getHoldView(int position, View contentView, ViewGroup parent) {

        return new TeamCircleHolder(contentView);
    }

    @Override
    public void setItemView(final int position, HolderView contentView, final CircleModel item) {
        TeamCircleHolder hold = (TeamCircleHolder) contentView;
        hold.content.setText(item.getCircleinfo());

        hold.name.setText(item.getCirclename());
        hold.createTime.setText(item.getCreatetime());

        if (isSearch) {
            hold.injoin.setVisibility(View.VISIBLE);
        } else {
            hold.injoin.setVisibility(View.GONE);
            if (item.getState() != 1) {
                hold.more.setVisibility(View.GONE);
            } else
                hold.more.setVisibility(View.VISIBLE);
        }
        hold.injoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (joinListener != null) {
                    view.setTag(position);
                    joinListener.onClick(view);
                }
            }
        });
        hold.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCirclePop(v, position, item);
            }

        });
    }

    private void showCirclePop(View v, final int position, final CircleModel item) {
        if (circleOptionsPopup == null) {
            circleOptionsPopup = new CircleOptionsPopup(ctx);
        }
        circleOptionsPopup.setTeamUser(item, position);
        if (item.getUserstate() != 1) {
            circleOptionsPopup.hideTeamsettlement();
        }
        circleOptionsPopup.showAtLocation(v, Gravity.CENTER, 0, 0);
    }

    public class TeamCircleHolder extends HolderView {
        private ImageView more;
        private TextView name;
        private TextView content;
        private TextView createTime;
        private TextView injoin;

        public TeamCircleHolder(View view) {
            more = (ImageView) view.findViewById(R.id.item_team_circle_more);
            content = (TextView) view.findViewById(R.id.item_team_content);
            createTime = (TextView) view.findViewById(R.id.create_time);
            name = (TextView) view.findViewById(R.id.create_man);
            injoin = (TextView) view.findViewById(R.id.item_team_request_injoin);

        }
    }
}
